package com.cssw.fyzb.busi.api.team;

import com.cssw.fyzb.busi.api.user.ListFans;
import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.busi.service.team.TeamService;
import com.cssw.fyzb.busi.util.TimeUtil;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.vo.user.PublicUserInfoVo;
import com.cssw.fyzb.core.model.vo.user.TeamDateCountVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据日期统计我的团队人数(最近30天)
 *
 * @author asa
 */
@Function(description = "根据日期统计团队(最近30天)", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true)
}, result = @Result(
        properties = {
                @Property(name = "total", type = Type.Integer, description = "总页数"),
                @Property(name = "count", type = Type.Integer, description = "总行数"),
        },
        listProperties = {
                @ListProperty(name = "list", properties = {
                        @Property(name = "date", type = Type.String, description = "日期yyyy-MM-dd"),
                        @Property(name = "num", type = Type.Integer, description = "人数")
                })
        }
))
@Component
public class ListTeamByDate implements Action{

    @Autowired
    private TeamService teamService;

    private static final Logger logger = LoggerFactory.getLogger(ListFans.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {

            List<Record> teamDates = teamService.listTeamForDate(userId, TimeUtil.getPastDate(30));

            List<TeamDateCountVo> list = ((Converter<List<Record>, List<TeamDateCountVo>>) users -> {
                List<TeamDateCountVo> vos = new ArrayList<>();
                users.forEach((v) -> {
                    TeamDateCountVo vo = new TeamDateCountVo();
                    vo.setDate(v.getStr("date"));
                    vo.setNum(v.getInt("num"));
                    vos.add(vo);
                });
                return vos;
            }).convert(teamDates);
            return Response.success().put("list", list);
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }
}
